﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using smsclictr.automation;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Management;

namespace OpalisAddOn.TriggerActions
{
    [Activity("Trigger Agent Action", Description="Trigger SCCM Agent Actions", ShowFilters=false)]
    public class TriggerAgentAction : IActivity
    {
        private String targetComputer;
        private String userName;
        private string password;

        public void Design(IActivityDesigner designer)
        {
            designer.AddInput("TargetComputer").WithComputerBrowser();
            designer.AddInput("Action").WithListBrowser(new object[] 
            { "HW Inventory", "SW Inventory", "Heartbeat", "Collect Files", "Machine Policy", "User Policy", "MSI Source Update", "DCM Scan", "Metering Scan",
                "OOB Discovery", "Certificate Maintenance", "Location Refresh", "Refresh MP", "Timeout Requests", "Reset Policy", "Reset paused SWDist", "Delete orphaned cached packages", "Delete all cached packages", "Delete cached updates"  });
            designer.AddInput("UserName").NotRequired();
            designer.AddInput("Password").NotRequired().PasswordProtect();
            designer.AddInput("Full Inventory").WithBooleanBrowser().WithDefaultValue(false).NotRequired();
            designer.AddOutput("Action").AsString().WithDescription("The last triggered action");
        }

        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            SMSClient oClient;
            targetComputer = request.Inputs["TargetComputer"].AsString();
            userName = request.Inputs["UserName"].AsString();
            password = request.Inputs["Password"].AsString();

            string action = request.Inputs["Action"].AsString();
            bool bFull = false;

            try
            {
                bFull = request.Inputs["Full Inventory"].AsBoolean();
            }
            catch { }

            if (string.IsNullOrEmpty(userName))
            {
                oClient = new SMSClient(targetComputer);
            }
            else
            {
                oClient = new SMSClient(targetComputer, userName, password);
            }

            switch (action.ToLower())
            {
                case "hw inventory":
                    oClient.Schedules.HardwareInventory(bFull);
                    break;
                case "sw inventory":
                    oClient.Schedules.SoftwareInventory(bFull);
                    break;
                case "heartbeat":
                    oClient.Schedules.DataDiscovery();
                    break;
                case "collect files":
                    oClient.Schedules.SinvCollFile();
                    break;
                case "machine policy":
                    oClient.Schedules.RequestMachineAssignments();
                    break;
                case "user policy":
                    oClient.Schedules.UserPolicyReq();
                    break;
                case "msi source update":
                    oClient.Schedules.MSISourceUpdate();
                    break;
                case "dcm scan":
                    oClient.Schedules.DCMScan();
                    break;
                case "metering scan":
                    oClient.Schedules.SWMTRReportGen();
                    break;
                case "oob discovery":
                    oClient.Schedules.OOBDiscovery();
                    break;
                case "certificate maintenance":
                    oClient.Schedules.CertMaintenance();
                    break;
                case "location refresh":
                    oClient.Schedules.LSRefreshLocations();
                    break;
                case "refresh mp":
                    oClient.Schedules.LSRefreshDefaultMP();
                    break;
                case "timeout requests":
                    oClient.Schedules.LSTimeOutRequests();
                    break;
                case "reset policy":
                    oClient.ResetPolicy();
                    break;
                case "reset paused swdist":
                    oClient.ResetPausedSWDist();
                    break;
                case "delete orphaned cached packages":
                    oClient.CacheCleanupOrphanedCacheInfo();
                    oClient.CacheCleanupOrphanedPackages();
                    oClient.CacheCleanupOldPackages();
                    oClient.CacheCleanupALL();
                    break;
                case "delete all cached package":
                    oClient.CacheDelete();
                    break;
                case "delete cached updates":
                    oClient.CacheCleanupPatches();
                    break;
            }

            response.Publish("Action", action);

        }
    }

}
